/**
 * @description
 * @author ybjuejue
 * @createDate 2018/12/27/027
 */
;(function (_) {
    window.__$clockpage__ = function (storeInfo) {
        var store = {state: {}, actions: {}, mutations: {}};
        store.state = _.extend({}, store.state, storeInfo.state || {});
        store.actions = _.extend({}, store, store.actions, storeInfo.actions || {});
        store.mutations = _.extend({}, store, store.mutations, storeInfo.mutations || {});
        store = JSON.parse(JSON.stringify(store));
        var watcher = {};
        for (var key in store.state) {watcher[key] = [];}

        function setState(obj) {
            var sobj = typeof obj === 'object' ? obj : {};
            for (var key in sobj) {
                if (key in store.state) {
                    var old = JSON.stringify(store.state[key]);
                    var val = JSON.stringify(sobj[key]);
                    if (val !== old) {
                        store.state[key] = sobj[key];
                        if (key in watcher) {
                            watcher[key] = Object.prototype.toString.call(watcher[key]) === '[object Array]' ? watcher[key] : []
                            for (var s = 0; s < watcher[key].length; s++) {
                                if (typeof watcher[key][s].page && typeof watcher[key][s].fn === 'function') {
                                    watcher[key][s].fn.call(watcher[key][s].page, store.state[key], old)
                                } else {
                                    watcher[key].splice(s, 0);
                                    s--;
                                }
                            }
                        }
                    }
                }
            }
        }

        var clockux = {
            setState: function (obj) {
                setState(obj || {});
            },
            state: function (key) {
                var s = JSON.parse(JSON.stringify(store.state));
                if (arguments.length >= 1) {
                    s = s[key];
                }
                return s;
            },
            commit: function (type, data) {
                var fn = store.mutations[type];
                if (typeof fn === 'function' && arguments.length > 1) {
                    store.mutations[type]({
                        setState: setState,
                        commit: this.commit,

                    }, data)
                }
            },
            dispatch: function (type, data) {
                var fn = store.actions[type];
                if (typeof fn === 'function' && arguments.length > 1) {
                    store.actions[type](this, data)
                }
            },
            unwatch: function (page) {
                page = page ? page : null;
                return function (obj) {
                    var s = Object.prototype.toString.call(obj) === '[object Object]' ? obj : {}
                    for (var i in s) {
                        if (i in store.state) {
                            for (var j = 0; j < watcher[i].length; j++) {
                                if (watcher[i][j].page === page) {
                                    watcher[i][j].splice(j, 1);
                                    j--;
                                }
                            }
                        }
                    }
                }
            },
            watch: function (page) {
                page = page ? page : null;
                return function (obj) {
                    var s = Object.prototype.toString.call(obj) === '[object Object]' ? obj : {}
                    for (var i in s) {
                        if (i in store.state) {
                            var st = true;
                            for (var j = 0; j < watcher[i].length; j++) {
                                if (typeof s[i] !== 'function') {
                                    st = false;
                                    break
                                } else if (watcher[i][j].page === page && watcher[i][j].fn) {
                                    watcher[i][j].fn = s[i];
                                    st = false;
                                    break
                                }
                            }
                            if (st) {
                                watcher[i].push({
                                    page: page,
                                    fn: s[i]
                                })
                            }
                        }
                    }
                }
            }
        };

        function clockComponent(el, option) {
            var opt = _.extend({}, option);
            delete opt.watch;
            var cmpLife = _.life(el, opt);
            cmpLife.store = clockux;
            var watch = option.watch || {};
            cmpLife.$domel = $cmp(el) || $cmp('<div></div>');
            clockux.watch(this.$domel)(watch);
            return cmpLife;
        }

        return clockComponent;
    };
})(cmp);